<?php
/**
 * Created by PhpStorm.
 * User: DQ
 * Date: 2018/8/30
 * Time: 18:05
 */
function success_response($data = [])
{
    header('Content-Type: application/json;charset:utf-8');
    echo json_encode(['data' => $data, 'message' => 'success', 'code' => 0]);
    exit();
}

function error_response($data = [], $code = -1)
{
    header('Content-Type: application/json;charset:utf-8');
    echo json_encode(['data' => $data, 'message' => 'error', 'code' => $code]);
    exit();
}

function h_property_exists( $class, $property , $defualt = null) {
    if (is_null($class)) {
        return $defualt;
    }
    if (is_object($class)) {
        if ( property_exists( $class, $property ) ) {
            return $class->$property;
        }
    }
    if (is_array($class)) {
        if (array_key_exists($property, $class)) {
            return $class[$property];
        }
    }
    return $defualt;
}

function get_asin_or_sku_form_item($url)
{
    if (preg_match('#<a.*?>([0-9a-zA-Z]{1,})</a>#', $url, $matches)) {
        return $matches[1];
    }
    return '';
}

function get_percent_from_item($item)
{
    if (preg_match('#<nobr>(.*?)\%</nobr>#', $item, $matches)) {
        return $matches[1];
    }
    return 0;
}

function get_money_from_item($item)
{
    if (preg_match('#\$(.*)#', $item, $matches)) {
        return $matches[1];
    }
    return 0;
}